package com.runen.infinitemovement.model.api;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.activity.MainActivity;
import com.runen.infinitemovement.base.BaseAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.AdInfoEntity;
import com.runen.infinitemovement.model.entity.DynamicEntity;
import com.runen.infinitemovement.model.entity.GetTopIdEntity;
import com.runen.infinitemovement.model.entity.HozmDetailEntity;
import com.runen.infinitemovement.model.entity.HozomSurplusEntity;
import com.runen.infinitemovement.model.entity.InviteFriendsEntity;
import com.runen.infinitemovement.model.entity.JudgeBranchCheckEntity;
import com.runen.infinitemovement.model.entity.MYHozomListEntity;
import com.runen.infinitemovement.model.entity.MeInfoEntity;
import com.runen.infinitemovement.model.entity.MedalListEntity;
import com.runen.infinitemovement.model.entity.MessageEntity;
import com.runen.infinitemovement.model.entity.MyBodyDataEntity;
import com.runen.infinitemovement.model.entity.MyCertificateEntity;
import com.runen.infinitemovement.model.entity.MyDynamicListEntity;
import com.runen.infinitemovement.model.entity.MyFocusEntity;
import com.runen.infinitemovement.model.entity.MyHomeEntity;
import com.runen.infinitemovement.model.entity.MyMedalEntity;
import com.runen.infinitemovement.model.entity.NewColleagueEntity;
import com.runen.infinitemovement.model.entity.NextAllCompanyStructureEntity;
import com.runen.infinitemovement.model.entity.PersonEntity;
import com.runen.infinitemovement.model.entity.PictrueEntity;
import com.runen.infinitemovement.model.entity.PictureListEntity;
import com.runen.infinitemovement.model.entity.ShopInfoEntity;
import com.runen.infinitemovement.model.entity.SignInEntity;
import com.runen.infinitemovement.model.entity.StepForEntity;
import com.runen.infinitemovement.model.entity.TeamStructureEntity;
import com.runen.infinitemovement.model.entity.TodayStepForValueEntity;
import com.runen.infinitemovement.model.entity.TopCompanyEntity;
import com.runen.infinitemovement.model.entity.TopCompanyRecommendEntity;
import com.runen.infinitemovement.model.entity.UpdataInfo;
import com.runen.infinitemovement.model.entity.UserEntity;
import com.runen.infinitemovement.model.entity.UserInformationEntity;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.AppUtils;
import com.runen.rxnetlibrary.utils.IntentUtils;
import com.runen.rxnetlibrary.utils.JsonUtils;
import com.runen.rxnetlibrary.utils.SharePreferenceUtils;
import com.runen.rxnetlibrary.utils.Utils;

import java.util.Map;
import java.util.TreeMap;

/**
 * 用户相关接口
 *
 * @author ldh
 */
public class UserAPI extends BaseAPI {

    private static final String FILE = "saveUserInfo";

    public static final String USERINFO_KEY = "userInfo";
    /**
     * 广告信息
     */
    private static final String FILE_AD = "saveADInfo";

    public static final String AD_KEY = "ADInfo";

    /**
     * 保存用户登录信息
     *
     * @param userInfo
     * @return
     */
    public static void saveUserInfo(UserEntity userInfo) {
        SharePreferenceUtils.putString(FILE, Utils.getApp(), USERINFO_KEY, JsonUtils.toJson(userInfo));
    }

    /**
     * 获取用户登录信息 如token、id
     */
    public static UserEntity getUserInfo() {
        String jsonStr = SharePreferenceUtils.getString(FILE, Utils.getApp(), USERINFO_KEY);
        return JsonUtils.fromJson(jsonStr, UserEntity.class);
    }

    /**
     * 保存广告信息
     *
     * @param adInfoEntity
     */
    public static void saveAdInfo(AdInfoEntity adInfoEntity) {
        SharePreferenceUtils.putString(FILE_AD, Utils.getApp(), AD_KEY, JsonUtils.toJson(adInfoEntity));
    }

    /**
     * 获取广告信息
     */
    public static AdInfoEntity getADInfo() {
        String jsonStr = SharePreferenceUtils.getString(FILE_AD, Utils.getApp(), AD_KEY);
        return JsonUtils.fromJson(jsonStr, AdInfoEntity.class);
    }

    /**
     * 检查是否登陆中
     *
     * @return
     */
    public static boolean checkLogin() {
        UserEntity userEntity = getUserInfo();
        if (userEntity != null) {
            if (userEntity.data != null) {
                if (!TextUtils.isEmpty(userEntity.data.access_token))
                    return true;
            }
        }
        return false;
    }


    /**
     * 获取用户Id
     *
     * @return
     */
    public static String getUserId() {
        UserEntity userEntity = getUserInfo();
        if (userEntity != null) {
            if (null != userEntity.data) {
                if (!TextUtils.isEmpty(userEntity.data.userId)) {
                    return userEntity.data.userId;
                }
            }
        }
        return null;
    }

    /**
     * 获取用户类型
     *
     * @return
     */
    public static String getUserToken() {
        UserEntity userEntity = getUserInfo();
        if (userEntity != null) {
            if (userEntity.data != null) {
                if (null != userEntity.data.access_token) {
                    return userEntity.data.access_token;
                }
            }
        }
        return null;
    }

    /**
     * 清理登录信息
     *
     * @return
     */
    public static void clearLogin() {
        SharePreferenceUtils.clear(FILE, Utils.getApp());
    }

    /**
     * 清理登录信息
     *
     * @return
     */
    public static void clearAD() {
        SharePreferenceUtils.clear(FILE_AD, Utils.getApp());
    }

    /**
     * 注销当前用户
     */
    public void logout(Context mActivity) {
        AppUtils.exitApp();
        clearLogin();
        IntentUtils.redirect(MainActivity.class);
        if (mActivity instanceof Activity) {
            ((Activity) mActivity).finish();
        }
    }

    /**
     * 退出登录
     *
     * @param string
     * @param callback
     */
    public void outLogin(String string, BaseResultCallback<BaseResponseEntity<?>> callback) {
        get(Constant.OUT_LOGIN_ACTION + string, null, callback);
    }

    /**
     * 获取手机短信验证码
     *
     * @param mobile   手机号码
     * @param listener
     */
    public void getPhoneSMS(String mobile, BaseResultCallback<BaseResponseEntity<?>> listener) {
        postBody(Constant.PHONE_SMS_ACTION, mobile, listener);
    }

    /**
     * 验证码登录
     *
     * @param jsonBody
     * @param listener
     */
    public void onLogin(Map jsonBody, BaseResultCallback<UserEntity> listener) {
        post(Constant.PHONE_CODE_ACTION, jsonBody, listener);
    }

    /**
     * 意见反馈
     *
     * @param jsonBody
     * @param listener
     */
    public void suggestInfo(String jsonBody, BaseResultCallback<BaseResponseEntity<?>> listener) {
        postBody(Constant.SUGGEST_INFO_ACTION, jsonBody, listener);
    }

    /**
     * 获取消息信息
     *
     * @param listener
     */
    public void onGetMessage(BaseResultCallback<MessageEntity> listener) {
        get(Constant.MESSAGE_ACTION, null, listener);
    }

    /**
     * 我的动态消息
     *
     * @param listener
     */
    public void onGetMyDynamicList(String str, BaseResultCallback<MyDynamicListEntity> listener) {
        get(Constant.MY_DYNAMIC_MESSAGE_LIST_ACTION + str, null, listener);
    }

    /**
     * 获取系统通知
     *
     * @param srt
     * @param listener
     */
    public void onSystemList(String srt, BaseResultCallback<MyDynamicListEntity> listener) {
        get(Constant.SYSTEM_MESSAG_ACTION + srt, null, listener);
    }

    /**
     * 通知新同事列表
     *
     * @param srt
     * @param listener
     */
    public void onNewColleagueList(String srt, BaseResultCallback<NewColleagueEntity> listener) {
        get(Constant.NEW_COLLEAGUE_ACTION + srt, null, listener);
    }


    /**
     * 我的火种列表信息
     *
     * @param
     * @param callback
     */
    public void onHozomList(BaseResultCallback<MYHozomListEntity> callback) {
        get(Constant.MY_HOZOM_LIST_ACTION, null, callback);
    }

    /**
     * 用户剩余火种数
     *
     * @param callback
     */
    public void hozomSurplus(BaseResultCallback<HozomSurplusEntity> callback) {
        get(Constant.HOZOM_SURPLUS_ACTION, null, callback);
    }

    /**
     * 商品信息
     *
     * @param string
     * @param callback
     */
    public void hzomShop(String string, BaseResultCallback<ShopInfoEntity> callback) {
        get(Constant.SHOP_INFO_ACTION + string, null, callback);
    }

    /**
     * 火种明细
     *
     * @param string
     * @param callback
     */
    public void hozomDetail(String string, BaseResultCallback<HozmDetailEntity> callback) {
        get(Constant.HOZOM_DETAIL_ACTION + string, null, callback);
    }

    /**
     * 当天步数兑换
     *
     * @param callback
     */
    public void stepsFor(BaseResultCallback<BaseResponseEntity<?>> callback) {
        post(Constant.STEPS_FOR_ACTION, null, callback);
    }

    /**
     * 图片上传
     *
     * @param map
     * @param callback
     */
    public void putFile(TreeMap treeMap, TreeMap map, BaseResultCallback<PictrueEntity> callback) {
        postFile(Constant.FILE_UPLOAD_ACTION, treeMap, map, callback);
    }

    /**
     * 批量上传图拍你
     *
     * @param map
     * @param callback
     */
    public void putFiles(TreeMap treeMap, TreeMap map, BaseResultCallback<PictureListEntity> callback) {
        postFilesType(Constant.FILES_UPLOAD_ACTION, treeMap, map, callback);
    }

    /**
     * 获取身体数据
     *
     * @param callback
     */
    public void getMyBodyData(BaseResultCallback<MyBodyDataEntity> callback) {
        get(Constant.MY_BODY_DATA_ACTION, null, callback);
    }

    /**
     * 修改我的身体数据
     *
     * @param str
     * @param callback
     */
    public void putMyBodyData(String str, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(Constant.UPDATA_MY_BODY_DATA_ACTION, str, callback);
    }

    /**
     * 我的主页
     *
     * @param string
     * @param callback
     */
    public void getMyHome(String string, BaseResultCallback<MyHomeEntity> callback) {
        get(Constant.MY_HOME_ACTION + string, null, callback);
    }

    /**
     * 用户注册时获取公司的顶级ID
     *
     * @param callback
     */
    public void getTopId(BaseResultCallback<GetTopIdEntity> callback) {
        get(Constant.GET_TOP_ID, null, callback);
    }

//    /**
//     * 注册
//     *
//     * @param jsonBody
//     * @param listener
//     */
//    public void onRegister(String jsonBody, BaseResultCallback<BaseResponseEntity<?>> listener) {
//        postBody(Constant.REGISTER_ACTION, jsonBody, listener);
//    }
//
//    /**
//     * 三方登陆
//     *
//     * @param jsonBody
//     * @param listener
//     */
//    public void onThirdLogin(String jsonBody, BaseResultCallback<LoginInfoEtity> listener) {
//        postBody(Constant.THIRED_LOGIN_ACTION, jsonBody, listener);
//    }
//
//    /**
//     * 修改密码
//     *
//     * @param jsonBody
//     * @param listener
//     */
//    public void onUpdatePassword(Map jsonBody, BaseResultCallback<BaseResponseEntity<?>> listener) {
//        post(Constant.UPDTAE_PASSWORD_ACTION, jsonBody, listener);
//    }
//
//    /**
//     * 设置密码
//     *
//     * @param hashMap
//     * @param listener
//     */
//    public void onSetPassword(String hashMap, BaseResultCallback<BaseResponseEntity> listener) {
//        postBody(Constant.SETTING_PASSWORD_ACTION, hashMap, listener);
////        postBody("",jsonBody,listener);
//    }
//

//
//    /**
//     * 密码登录
//     *
//     * @param jsonBody
//     * @param listener
//     */
//    public void onLoginPassword(String jsonBody, BaseResultCallback<LoginInfoEtity> listener) {
//        postBody(Constant.LOGIN_PASSWORD_ACTION, jsonBody, listener);
//    }
//
//    /**
//     * 获取Ali token
//     */
//    public void getAliToken(BaseResultCallback<QiNiuTokenEntity> listener) {
//        get(Constant.ALI_TOKEN_ACTION, null, listener);
//    }
//
//    /**
//     * 绑定手机
//     *
//     * @param jsonBody
//     * @param listener
//     */
//    public void onBindingPhone(String jsonBody, BaseResultCallback<LoginInfoEtity> listener) {
//        postBody(Constant.BINDING_PHONR_ACTION, jsonBody, listener);
//    }
//
//    /**
//     * 登录成功后保存基本信息(app版本号,手机型号等)
//     *
//     * @param body
//     * @param listener
//     */
//    public void onSaveUserBase(String body, BaseResultCallback<BaseResponseEntity<?>> listener) {
//        postBody(Constant.SAVE_USER_BASE_ACTION, body, listener);
//    }
//

    /**
     * 获取用户信息
     *
     * @param
     * @param listener
     */
    public void onGetUserInfo(Map map, BaseResultCallback<UserInformationEntity> listener) {
        get(Constant.GET_USER_INFO_ACTION, map, listener);
    }

    /**
     * 保存修改后的基本信息
     *
     * @param body
     * @param listener
     */
    public void onSavePerson(String body, BaseResultCallback<BaseResponseEntity<?>> listener) {
        postBody(Constant.SAVE_PERSON_INFO_ACTION, body, listener);
    }

    /**
     * 我的页面数据信息
     *
     * @param callback
     */
    public void meInfo(BaseResultCallback<MeInfoEntity> callback) {
        get(Constant.ME_INFO_ACTION, null, callback);
    }


    /**
     * 过去顶级公司
     *
     * @param callback
     */
    public void getTopCompany(String str, BaseResultCallback<TopCompanyEntity> callback) {
        get(Constant.TOP_COMPANY_ACTION + str, null, callback);
    }

    /**
     * 获取公司组织
     *
     * @param callback
     */
    public void getNextAllCompany(String string, BaseResultCallback<NextAllCompanyStructureEntity> callback) {
        get(Constant.NEXT_COMPANY_ACTION + string, null, callback);
    }

    public void getNextAllCompany2(String string, BaseResultCallback<TeamStructureEntity> callback) {
        get(Constant.NEXT_COMPANY_ACTION + string, null, callback);
    }

    /**
     * SUBMIT_BRANCH_ACTION
     *
     * @param str
     * @param callback
     */
    public void submitBranch(String str, int type, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(type == 1 ? Constant.CHANGE_BRANCH_ACTION : Constant.SUBMIT_BRANCH_ACTION, str, callback);
    }

    /**
     * 切换组织
     *
     * @param str
     * @param callback
     */
    public void checkBranch(String str, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(Constant.CHECK_BRANCH_ACTION, str, callback);
    }

    /**
     * 判断加入申请状态
     *
     * @param callback
     */
    public void judgeCheck(BaseResultCallback<JudgeBranchCheckEntity> callback) {
        get(Constant.JUDGE_CHECK_ACTION, null, callback);
    }

    /**
     * 获取我的关注列表
     *
     * @param str
     * @param callback
     */
    public void getMyFocus(String str, BaseResultCallback<MyFocusEntity> callback) {
        get(Constant.GET_MY_FOCUS_ACTION + str, null, callback);
    }

    /**
     * 取消关注
     *
     * @param string
     * @param callback
     */
    public void cancelFocus(String string, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(Constant.CANCEL_FOCUS_ACTION, string, callback);
    }

    /**
     * 添加关注
     *
     * @param string
     * @param callback
     */
    public void addFocus(String string, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(Constant.ADD_FOCUS_ACTION, string, callback);
    }

    /**
     * 签到
     *
     * @param
     * @param listener
     */
    public void onSignIn(BaseResultCallback<SignInEntity> listener) {
        post(Constant.SIGN_IN_ACTION, null, listener);
    }
//
//    /**
//     * 更改手机号
//     *
//     * @param body
//     * @param listener
//     */
//    public void onAlterPhone(String body, BaseResultCallback<BaseResponseEntity<?>> listener) {
//        postBody(Constant.ALTER_PHPNE_ACTION, body, listener);
//    }
//
//    /**
//     * 更改密码
//     *
//     * @param body
//     * @param listener
//     */
//    public void onAlterPassword(Map body, BaseResultCallback<BaseResponseEntity<?>> listener) {
//        post(Constant.ALTER_PASSWORD_ACTION, body, listener);
//    }
//
//    /**
//     * 获取用户账号信息
//     *
//     * @param listener
//     */
//    public void onAccount(BaseResultCallback<GetAccountInfoEntity> listener) {
//        get(Constant.ACCOUNT_ACTION, null, listener);
//    }
//
//    /**
//     * 解除三方绑定
//     *
//     * @param listener
//     */
//    public void onRemoveThirdBinding(String body, BaseResultCallback<GetAccountInfoEntity> listener) {
//        postBody(Constant.THIRD_REMOVE_BINDING_ACTION, body, listener);
//    }
//
//    /**
//     * 三方绑定
//     *
//     * @param body
//     * @param listener
//     */
//    public void onThirdBinding(String body, BaseResultCallback<BaseResponseEntity<?>> listener) {
//        postBody(Constant.THIRD_BINDING_ACTION, body, listener);
//    }
//

    /**
     * 保存极光的regId
     *
     * @param map
     * @param listener
     */
    public void onUserRegId(String map, BaseResultCallback<BaseResponseEntity<?>> listener) {
        postBody(Constant.USER_REG_ID_ACTION, map, listener);
    }
//

    /**
     * 版本更新
     *
     * @param listener
     */
    public void getUpdata(BaseResultCallback<UpdataInfo> listener) {
        get(Constant.GET_UPDATA_ACTION, null, listener);
    }

    /**
     * 我的证书列表
     *
     * @param listener
     */
    public void getMyCertificate(BaseResultCallback<MyCertificateEntity> listener) {
        get(Constant.MY_CERTIFICATE_ACTION, null, listener);
    }

    /**
     * 获取我的微章
     *
     * @param listener
     */
    public void getMyMedal(String type, BaseResultCallback<MyMedalEntity> listener) {
        get(Constant.MY_MEDAL_ACTION + type, null, listener);
    }

    /**
     * 获取邀请好友列表
     *
     * @param callback
     */
    public void getInviteFriends(BaseResultCallback<InviteFriendsEntity> callback) {
        get(Constant.INVITE_FRIENDS_ACTION, null, callback);
    }

    /**
     * 上传步数
     *
     * @param string
     * @param callback
     */
    public void upDataStep(String string, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(Constant.UP_DATA_STEP_ACTION, string, callback);
    }

    /**
     * 查询当前步数
     *
     * @param callback
     */
    public void getStep(BaseResultCallback<TodayStepForValueEntity> callback) {
        get(Constant.CHECK_STEP_ACTION, null, callback);
    }

    /**
     * 兑换步数
     *
     * @param callback
     */
    public void stepFor(BaseResultCallback<StepForEntity> callback) {
        post(Constant.STEP_FOR_ACTION, null, callback);
    }

    /**
     * 广告信息
     *
     * @param callback
     */
    public void adInfo(BaseResultCallback<AdInfoEntity> callback) {
        get(Constant.AD_ACTION, null, callback);
    }

    /**
     * 获取推荐一级团队列表
     *
     * @param callback
     */
    public void getCompanyRecommend(BaseResultCallback<TopCompanyRecommendEntity> callback) {
        get(Constant.GET_COMPANY_APP_TEAMFIRSTLIST, null, callback);
    }

    /**
     * 上传应用版本号
     *
     * @param params
     * @param callback
     */
    public void postAppVersion(String params, BaseResultCallback<BaseResponseEntity> callback) {
        postBody(Constant.POST_APP_VERSION, params, callback);
    }

    /**
     * 获取个人主页信息
     *
     * @param params
     * @param callback
     */
    public void getPersonPagerInfo(String params, BaseResultCallback<PersonEntity> callback) {
        get(Constant.PERSON_PAGE_INFO + params, null, callback);
    }

    /**
     * 获取个人主页信息
     *
     * @param params
     * @param callback
     */
    public void getUserDynamicList(String params, BaseResultCallback<DynamicEntity> callback) {
        get(Constant.GET_USER_DYNAMIC_LIST + params, null, callback);
    }

    /**
     * 获取荣耀勋章
     *
     * @param params
     * @param callback
     */
    public void getUserMedalList(String params, BaseResultCallback<MedalListEntity> callback) {
        get(Constant.GET_USER_MEDAL_LIST + params, null, callback);
    }

    /**
     * 判断用户是否可以切换1级军团
     *
     * @param callback
     */
    public void getWhetherSwitchCompany(BaseResultCallback<BaseResponseEntity> callback) {
        get(Constant.GET_WHETHER_SWITCH_COMPANY, null, callback);
    }
}
